package com.qf.qfsy2203.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * 文件追加器
 */
public class QfFileAppender {

    // 文件的绝对路径
    String filepath;

    BufferedWriter bufferedWriter ;

    public void setFilepath(String filepath) throws IOException {
        this.filepath = filepath;

        // 初始化 bufferedWriter
        bufferedWriter= new BufferedWriter(new FileWriter(filepath));
    }

    /**
     * 把 list中的 字符串写入指定文件
     * @param data
     */
    public void append(List<String> data) throws IOException {

        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
    }

    /**
     * 关闭 文件追加器对象
     */
    public void close() throws IOException {
        bufferedWriter.close();
    }

}
